﻿using Comon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ThoughtWorks.QRCode.Codec;
 
namespace QC
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 开始操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {

            int row = dataGridView1.Rows.Count;//得到总行数 
            if(row>0){
                for (int i = 0; i < row-1; i++)//得到总行数并在之内循环    
                {
                    string urlinfo = dataGridView1.Rows[i].Cells[0].Value.ToString();

                    //五色集合
                    List<Color> colorList = new List<Color>();
                    colorList.Add(Color.Green);
                    colorList.Add(Color.Red);
                    colorList.Add(Color.Blue);
                    colorList.Add(System.Drawing.ColorTranslator.FromHtml("#E01D72"));//枚红色
                    colorList.Add(Color.Brown);


                    QrCodeHelper codeCreate = new QrCodeHelper();

                    string content = "http://wx.nb12315.com/Hxmy/fw.aspx?code=" + urlinfo;
                     
                        //生成四张彩色二维码图片
                        //五种颜色基色
                        //红色 绿色 蓝色 玫红色 褐色
                        //红色 绿色 蓝色 玫红色 橙黄色
                        Random ra = new Random();
                        int[] colorInts = new int[4];
                        for (var w = 0; w < 4; w++)
                            colorInts[w] = ra.Next(0, 4);
                        List<Brush> brushList = new List<Brush>();
                        Image img1 = codeCreate.Create_ImgCode(content, 4, colorList[colorInts[0]]);
                        //创建对应颜色
                        var b1 = new SolidBrush(colorList[colorInts[0]]);
                        brushList.Add(b1);

                        Image img2 = codeCreate.Create_ImgCode(content, 4, colorList[colorInts[1]]);

                        var b2 = new SolidBrush(colorList[colorInts[1]]);
                        brushList.Add(b2);

                        Image img3 = codeCreate.Create_ImgCode(content, 4, colorList[colorInts[2]]);

                        var b3 = new SolidBrush(colorList[colorInts[2]]);
                        brushList.Add(b3);



                        Image img4 = codeCreate.Create_ImgCode(content, 4, colorList[colorInts[3]]);

                        var b4 = new SolidBrush(colorList[colorInts[3]]);
                        brushList.Add(b4);

                        //分别切除四个图片的四个角 获得新的四个小图片信息
                        Image img5 = codeCreate.CutImage(img1, 0, 0, img1.Width / 2, img1.Height / 2);
                        Image img6 = codeCreate.CutImage(img2, img1.Width / 2, 0, img2.Width / 2, img2.Height / 2);
                        Image img7 = codeCreate.CutImage(img3, 0, img1.Height / 2, img3.Width / 2, img3.Height / 2);
                        Image img8 = codeCreate.CutImage(img4, img1.Width / 2, img2.Height / 2, img4.Width / 2, img4.Height / 2);
                        img1.Dispose(); img1 = null;
                        img2.Dispose(); img2 = null;
                        img3.Dispose(); img3 = null;
                        img4.Dispose(); img4 = null;
                        ////拼接四张图片信息
                        Image img9 = codeCreate.JoinImage(img5, img6, img7, img8, "D:\\Upload\\\\fina" + i + ".jpg");
                        img5.Dispose(); img5 = null;
                        img6.Dispose(); img6 = null;
                        img7.Dispose(); img7 = null;
                        img8.Dispose(); img8 = null;

                        ////创建对应的颜色的彩色字体信息
                        Image imgFont = codeCreate.ColorFont(brushList, urlinfo, 5);

                        //图片拼接
                        codeCreate.JoinImage(imgFont, img9, "D:\\Upload\\\\fina" + i + ".jpg");

 

                }


            }
            

            MessageBox.Show("成功操作！");
          

        }
        /// <summary>
        /// 导码对话框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void importCode_Click(object sender, EventArgs e)
        {
            DataTable tbl = new DataTable();
            tbl.Columns.Add("UrlInfo",typeof(string));
            //定义一个文件打开控件
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            { 
                //成功选择文件过后操作
                //获取用户选择的文件完整路径
                string filePath = ofd.FileName;
                //获取对话框中所选文件的文件名和扩展名，文件名不包括路径
                string fileName = ofd.SafeFileName;
                StreamReader sr = new StreamReader(filePath, Encoding.UTF8);
                String line;
                while ((line = sr.ReadLine()) != null)
                {
                    DataRow dr = tbl.NewRow();
                    dr[0] = line;
                    tbl.Rows.Add(dr);
                }

                this.dataGridView1.DataSource = tbl;
                 
            }
        }
    }
}
